package com.helen.ui.recoveryclass;

import android.content.Intent;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.coorchice.library.SuperTextView;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.helen.adapter.RecoveryClassListAdapter;
import com.helen.entity.RecoveryClassEntity;
import com.helen.global.Constants;
import com.helen.shopping.R;
import com.helen.ui.BaseActivity;
import com.helen.utils.MyLog;
import com.helen.utils.PhoneUtils;
import com.helen.utils.tiputils.MToast;
import com.zhouyou.http.EasyHttp;
import com.zhouyou.http.cache.converter.SerializableDiskConverter;
import com.zhouyou.http.cache.model.CacheMode;
import com.zhouyou.http.cache.model.CacheResult;
import com.zhouyou.http.callback.SimpleCallBack;
import com.zhouyou.http.exception.ApiException;
import com.zhouyou.http.model.HttpParams;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by yang on 2018/5/2.
 * 回收商品分类
 */

public class RecoveryClassActivity extends BaseActivity {
    @BindView(R.id.rv_list)
    RecyclerView rvList;
    @BindView(R.id.swipe_refresh)
    SwipeRefreshLayout swipeRefresh;
    @BindView(R.id.tv_sure)
    SuperTextView tvSure;
    private List<RecoveryClassEntity> list = new ArrayList();
    private RecoveryClassListAdapter recoveryClassListAdapter;
    private View errorView;
    private int iid = 1;
    private ArrayList<Integer> selectId = new ArrayList<>();
    private List<RecoveryClassEntity> selectedDataList = new ArrayList<>();
    private RecoveryClassEntity recoveryClassEntity;
    private int id = 0;

    @Override
    protected int attachLayoutRes() {
        return R.layout.activity_recovery_class;
    }

    @Override
    protected void initInjector() {

    }

    @Override
    protected void initViews() {
        getToolbarTitle().setText(R.string.select_class);
        iid = getIntent().getIntExtra("iid", 1);
//        recoveryClassEntity = (RecoveryClassEntity) getIntent().getSerializableExtra("recoveryClassEntity");
        selectedDataList = (List<RecoveryClassEntity>) getIntent().getSerializableExtra("selectedDataList");
//        if (recoveryClassEntity != null) {
//            id = recoveryClassEntity.getId();
//        }
        if(selectedDataList!=null && selectedDataList.size()>0){
            for (int i = 0; i < selectedDataList.size(); i++) {
                selectId.add(selectedDataList.get(i).getId());
            }
        }
        errorView = getLayoutInflater().inflate(R.layout.error_view, (ViewGroup) rvList.getParent(), false);
        recoveryClassListAdapter = new RecoveryClassListAdapter(R.layout.item_recovery_class_list, list, selectId, selectedDataList);
//        recoveryClassListAdapter = new RecoveryClassListAdapter(R.layout.item_recovery_class_list, list,id);
        recoveryClassListAdapter.openLoadAnimation(BaseQuickAdapter.SLIDEIN_BOTTOM);
        swipeRefresh.setRefreshing(true);
        swipeRefresh.setColorSchemeColors(getResources().getColor(R.color.appThemeColor));
        rvList.setLayoutManager(new GridLayoutManager(RecoveryClassActivity.this, 3));
        rvList.setAdapter(recoveryClassListAdapter);
        swipeRefresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                refresh();
            }
        });


//        recoveryClassListAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
//            @Override
//            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
//                if (recoveryClassListAdapter.getSelectedPostion() == position) {
//                    recoveryClassListAdapter.setSelectedPostion(-1);
//                    recoveryClassEntity = null;
//                } else {
//                    recoveryClassListAdapter.setSelectedPostion(position);
//                    recoveryClassEntity = (RecoveryClassEntity) adapter.getData().get(position);
//                }

//                recoveryClassEntity = (RecoveryClassEntity) adapter.getData().get(position);
//                recoveryClassListAdapter.setId(recoveryClassEntity.getId());
//                recoveryClassListAdapter.notifyDataSetChanged();
//            }
//        });

        recoveryClassListAdapter.setSelectIdListener(new RecoveryClassListAdapter.SelectIdListener() {
            @Override
            public void onSelectIdChange(ArrayList<Integer> selectData, List<RecoveryClassEntity> mSelectedDataList) {
                selectId = selectData;
                selectedDataList = mSelectedDataList;
            }
        });

        refresh();
    }

    @Override
    protected void updateViews() {

    }

    private void refresh() {
        recoveryClassListApi();
    }

    private void recoveryClassListApi() {
        mMProgressDialog.show();
        HttpParams params = new HttpParams();
        params.put("iid", iid + "");
        EasyHttp.post(Constants.API_JSONAPI_RECOVERYCLASSLIST)
                .cacheMode(CacheMode.CACHEANDREMOTEDISTINCT)
                .cacheKey(Constants.API_JSONAPI_RECOVERYCLASSLIST + "_" + iid)
                .cacheDiskConverter(new SerializableDiskConverter())
                .params(params)
                .execute(new SimpleCallBack<CacheResult<String>>() {
                    @Override
                    public void onCompleted() {
                        super.onCompleted();
                        mMProgressDialog.dismiss();
                        swipeRefresh.setRefreshing(false);
                    }

                    @Override
                    public void onError(ApiException e) {
                        MyLog.d(TAG, "回收分类列表失败==" + e.toString());
                        MToast.makeTextShort(RecoveryClassActivity.this, getString(R.string.service_error)).show();
                    }

                    @Override
                    public void onSuccess(CacheResult<String> stringCacheResult) {
                        String data = PhoneUtils.checkResponseFlag2(RecoveryClassActivity.this, stringCacheResult.data, TAG);
                        if (data != null && !data.equals("[]")) {
                            list = new Gson().fromJson(data, new TypeToken<List<RecoveryClassEntity>>() {
                            }.getType());
                            recoveryClassListAdapter.setNewData(list);
                        } else {
                            recoveryClassListAdapter.setEmptyView(errorView);
                        }


                    }
                });
    }


    @OnClick(R.id.tv_sure)
    public void onViewClicked() {
        if (selectedDataList == null || selectedDataList.size() == 0) {
            MToast.makeTextLong(RecoveryClassActivity.this, "请最少选择一种分类").show();
            return;
        }

        for (int i = 0; i < selectedDataList.size(); i++) {
            MyLog.e("yang", "id==" + selectedDataList.get(i).getId());
        }


        Intent ti = new Intent();
        ti.putExtra("selectedDataList", (Serializable) selectedDataList);
        setResult(RESULT_OK, ti);
        finish();

//        if (recoveryClassEntity == null) {
//            MToast.makeTextLong(RecoveryClassActivity.this, "请选择一种分类").show();
//            return;
//        }

//        Intent ti = new Intent();
//        ti.putExtra("recoveryClassEntity",recoveryClassEntity);
//        setResult(RESULT_OK,ti);
//        finish();

    }
}
